package com.samp.bblock.test.controller;


import com.samp.bblock.core.base.BblockResponse;
import com.samp.bblock.util.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Controller
public class StatusController {

//	@Value("#{prop.baseNasPath}")
	private String baseNasPath;

//	@Value("#{prop.zkServerAddress}")
	private String zkServerAddress;

	private static final String STATUS_OK = "正常";
	private static final String STATUS_ERROR = "异常";

	private static final Logger logger = LoggerFactory.getLogger(StatusController.class);

	// 保存用户信息信息
	@SuppressWarnings("rawtypes")
	@RequestMapping("/status/heartbeat.do")
	@ResponseBody
	public BblockResponse heartbeat() {
			BblockResponse response = new BblockResponse();
			response.setCode(Constants.RESPONSE_CODE_OK);
			response.setMessage("成功");
			logger.info("call heartbeat and return ok!");
			return response;
	}

	@RequestMapping("/status/checkComponentStatus.do")
	@ResponseBody
	public BblockResponse checkComponentStatus(@RequestParam(required=false)  String paramName) {
		BblockResponse response = new BblockResponse();
		response.setCode(Constants.RESPONSE_CODE_OK);
		response.setMessage("成功");
		List<ComponentInfo> statusList = new ArrayList<>();
		statusList.add(checkDB());
		statusList.add(checkNas());
		response.setList(statusList);
		return response;
	}

	/**
	 * 检查数据库连接。
	 * @return
	 */
	private ComponentInfo checkDB(){
		long start = System.currentTimeMillis();
		ComponentInfo info = new ComponentInfo();
		info.setName("db");
		//检查数据库连接
		info.setStatus(STATUS_OK);
		try {
//			check db
		}catch (Exception e){
			logger.error("检查数据库状态失败。", e);
			info.setStatus(STATUS_ERROR);
			info.setMessage(e.getMessage());
		}
		long end = System.currentTimeMillis();
		info.setCost(end-start);
		return info;
	}

	private ComponentInfo checkNas(){
		//检查nas状态
		long start = System.currentTimeMillis();
		ComponentInfo info = new ComponentInfo();
		info.setName("nas");
		info.setStatus(STATUS_OK);
		info.setMessage("NAS:"+baseNasPath);
		try {
			String tempDir = baseNasPath+"temp";
			File file = new File(tempDir);
			if( !file.exists() ){
				if( !file.mkdirs() ) {
					info.setStatus(STATUS_ERROR);
					info.setMessage("创建文件夹失败");
				}
			}
		}catch (Exception e){
			logger.error("检查NAS失败。", e);
			info.setStatus(STATUS_ERROR);
			info.setMessage(e.getMessage());
		}
		long end = System.currentTimeMillis();
		info.setCost(end-start);
		return info;
	}


	
}

class ComponentInfo{
	private String name;
	private String status;
	private long cost;
	private String message;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public long getCost() {
		return cost;
	}

	public void setCost(long cost) {
		this.cost = cost;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}